package com.crm.web.util;

import com.crm.service.IListboxService;

import java.text.SimpleDateFormat;
import java.text.ParseException;
import java.util.Locale;
import java.util.Date;
import java.util.List;
import java.util.Calendar;

/**
 * @author <a href="mailto:huang0510@hotmail.com">HuangHuang</a>
 * @version $Id$
 */
public class Tools {
    /**
     * appointed format of date
     */
    public static final String DATEANDTIME = "yyyy-MM-dd";

    /**
     * date convert to char
     *
     * @param date    Date The date
     * @param pattern String format
     * @return String The char after converted
     */

    public static String formatDate(Date date, String pattern) {
        SimpleDateFormat formatter = new SimpleDateFormat(pattern, Locale.getDefault());
        return formatter.format(date);
    }

    public static int getTotalPage(double amountInEachPage, List items) {
        double pages = Math.ceil(items.size() / amountInEachPage);

        System.out.println("pages:" + pages);
        return (int) pages;
    }

    public static String getRemindInfo(int dayNum) {
        Calendar tempDate = Calendar.getInstance();
        for (int f = 0; f <= dayNum; f++) {
            tempDate = Calendar.getInstance();
            tempDate.add(Calendar.DATE, f);
            if (tempDate.get(Calendar.DAY_OF_WEEK) == 7 || tempDate.get(Calendar.DAY_OF_WEEK) == 1) dayNum++;
        }
        int month = tempDate.get(Calendar.MONTH) + 1;
        String remindDate = tempDate.get(Calendar.YEAR) + "-" + month + "-" + tempDate.get(Calendar.DATE);
        System.out.println("INFO: To produce remind date is  -> " + remindDate);
        return remindDate;
    }

    /**
     * Get a Date object by date string
     *
     * @param dateStr Date String
     * @return Date object
     * @throws ParseException parse date String exception
     */
    public static Date getDateByString(String dateStr) throws ParseException {
        SimpleDateFormat dateFormat = new java.text.SimpleDateFormat("yyyy-MM-dd");
        return dateFormat.parse(dateStr);
    }

    /**
     * Get a date interval by two date String
     *
     * @param dateString      Date String
     * @param otherDateString Other date string
     * @return interval of two date
     * @throws ParseException parse date String exception
     */
    public static long getDateInterval(String dateString, String otherDateString) throws ParseException {
        Date date = getDateByString(dateString);
        Date otherDate = getDateByString(otherDateString);
        long interval = date.getTime() - otherDate.getTime();
        return interval / (1000 * 60 * 60 * 24);
    }

    public static void main(String[] args) {
    }
}
